package io.github.wslxm.springbootplus2.manage.gc.generate.gcimpl;

import io.github.wslxm.springbootplus2.manage.gc.config.GcConfig;
import io.github.wslxm.springbootplus2.manage.gc.constant.VueVersionConstant;
import io.github.wslxm.springbootplus2.manage.gc.generate.util.GenerateUtil;
import io.github.wslxm.springbootplus2.manage.gc.generate.util.GenerateVue2Util;
import io.github.wslxm.springbootplus2.manage.gc.generate.util.GenerateVue3Util;
import io.github.wslxm.springbootplus2.manage.gc.model.po.DbFieldPO;
import io.github.wslxm.springbootplus2.manage.gc.generate.GcSevice;
import io.github.wslxm.springbootplus2.manage.gc.utils.GcDataUtil;
import io.github.wslxm.springbootplus2.manage.gc.utils.GcFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@SuppressWarnings("all")
@Component
@Slf4j
public class GcVueAdd extends BaseGcImpl implements GcSevice {

    /**
     * 模板key
     */
    public static final String KEY_NAME = "X-VueAdd";

    public static final String PID_KEY = "pid";

    /**
     * 生成Html-Add 添加页
     *
     * @param data    数据
     * @param GenerateConfig 数据
     * @param path    生成代码路径
     * @return void
     * @date 2019/11/20 19:18
     */
    @Override
    public void run(GcConfig gcConfig) {
        log.info("开始生成: {}", KEY_NAME);
        StringBuffer vueAddColumnsDefault = new StringBuffer("");
        StringBuffer vueAddColumns = new StringBuffer("");
        StringBuffer vueAddColumnSlots = new StringBuffer("");
        //
        List<DbFieldPO> dbFields = gcConfig.getDbFields();
        for (DbFieldPO dbField : dbFields) {
            // 未勾选的字段过滤
            if (!GenerateUtil.isChecked(dbField)) {
                continue;
            }
            // 不生成id
            if ("id".equals(dbField.getName())) {
                continue;
            }
            // 生成字段默认值
            String name = GcDataUtil.getFieldName(gcConfig, dbField.getName());
            vueAddColumnsDefault.append("                    " + name + ": " + null + ",\n");

            if (VueVersionConstant.VUE3.equals(gcConfig.getVueVersion())) {
                // 生成字段
                vueAddColumns.append(GenerateVue3Util.jxVue3AddOrUpdColumns(
                        gcConfig,
                        dbField.getName(),
                        dbField.getType(),
                        dbField.getTypeDetail(),
                        GenerateUtil.getDesc(dbField.getDesc(), dbField.getName()),
                        dbField.getVueFieldType(),
                        dbField.getDictCode(),
                        dbField.getIsNull()));
                // 生成字段 插槽
                vueAddColumnSlots.append(GenerateVue3Util.jxVue3AddOrUpdColumnsSlot(dbField.getVueFieldType(), name));
            } else {
                // 生成字段
                vueAddColumns.append(GenerateVue2Util.jxVue2AddOrUpdColumns(
                        gcConfig,
                        dbField.getName(),
                        dbField.getType(),
                        dbField.getTypeDetail(),
                        GenerateUtil.getDesc(dbField.getDesc(), dbField.getName()),
                        dbField.getVueFieldType(),
                        dbField.getDictCode(),
                        dbField.getIsNull()));
                // 生成字段 插槽
                vueAddColumnSlots.append(GenerateVue2Util.jxVue2AddOrUpdColumnsSlot(dbField.getVueFieldType(), name));
            }

        }
        // 数据保存
        gcConfig.setTemplateParam("vueAddColumnsDefault", vueAddColumnsDefault.toString());
        gcConfig.setTemplateParam("vueAddColumns", vueAddColumns.toString());
        gcConfig.setTemplateParam("vueAddColumnSlots", vueAddColumnSlots.toString());
        // 开始生成文件并进行数据替换
        GcFileUtil.replacBrBwWritee(gcConfig, GcFileUtil.getBrBwPath(gcConfig, KEY_NAME));
    }
}
